import axios from "axios";

export function requireData(url, params, type, item) {

    if (!url) return false;
    switch (item) {
        case "API":
            url = axios.defaults.baseApiURL + url
            break;
    }
    if (type === 'get') {
        return new Promise((resolve, reject) => {
            axios.get(url, {params: params}).then(res => {
                if (res) {
                    resolve(res.data)
                }
            }).catch(err => {
                reject(err)
            })
        })
    } else if (type === 'post') {
        return new Promise((resolve, reject) => {
            axios.post(url, params).then(res => {
                if (res) {
                    resolve(res.data)
                }
            }).catch(err => {
                console.log(err)
            })
        })
    } else if (type === 'upload') {
        return new Promise((resolve, reject) => {
            let config = {
                // 新增请求头
                headers: {"Content-type": "multipart/form-data"},
                // 新增上传进入监听事件
                onUploadProgress: e => {
                    var completeProgress = ((e.loaded / e.total * 100) | 0) + '%';
                    this.progress = completeProgress;
                }
            }
        })
    } else if (type === 'delete') {
        return new Promise((resolve, reject) => {
            axios.delete(url, {params: params}).then(res => {
                if (res) {
                    resolve(res.data)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }
}
